package com.ruoyi.modules.monitor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysJob;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtils;
import com.ruoyi.modules.system.service.ISysJobService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 定时任务
 *
 * @author ruoyi
 * @date 2025-06-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/monitor/job")
public class SysJobController extends BaseController {

    private final ISysJobService iSysJobService;

    /**
     * 查询定时任务列表
     */
    @SaCheckPermission("monitor:job:list")
    @GetMapping("/list")
    public R<Page<SysJob>> list(SysJob bo, PageQuery pageQuery) {
        Page<SysJob> page = iSysJobService.queryPageList(bo, pageQuery);
        return R.ok(page);
    }

    /**
     * 导出定时任务列表
     */
    @SaCheckPermission("monitor:job:export")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysJob bo, HttpServletResponse response) {
        List<SysJob> list = iSysJobService.queryList(bo);
        ExcelUtils.exportExcel(list, "定时任务", SysJob.class, response);
    }

    /**
     * 获取定时任务详细信息
     *
     * @param jobId 主键
     */
    @SaCheckPermission("monitor:job:query")
    @GetMapping("/{jobId}")
    public R<SysJob> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long jobId) {
        return R.ok(iSysJobService.queryById(jobId));
    }

    /**
     * 新增定时任务
     */
    @SaCheckPermission("monitor:job:add")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysJob bo) {
        return toAjax(iSysJobService.insertByBo(bo));
    }

    /**
     * 修改定时任务
     */
    @SaCheckPermission("monitor:job:edit")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysJob bo) {
        return toAjax(iSysJobService.updateByBo(bo));
    }

    /**
     * 删除定时任务
     *
     * @param jobIds 主键串
     */
    @SaCheckPermission("monitor:job:remove")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] jobIds) {
        return toAjax(iSysJobService.deleteWithValidByIds(Arrays.asList(jobIds), true));
    }

    /**
     * 修改定时任务状态
     */
    @SaCheckPermission("monitor:job:edit")
    @Log(title = "定时任务状态", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@Validated(EditGroup.class) @RequestBody SysJob bo) {
        return toAjax(iSysJobService.updateStatus(bo));
    }
}
